
var currentPage = 0;
var count=20;
var actiname="all";

getAllUser(currentPage);
getAllActivity();

$(function() {　　
	$("#nextPage").click(function() {　　　　
		currentPage++;
		if(actiname=="all")
		{
			getAllUser(currentPage);　
		}
		else
		{
			selectUserByActivity(currentPage,actiname)
		}
		　
	});

	$("#prePage").click(function() {　　　　
		if(currentPage <= 0) {
			return;
		}
		currentPage--;
		if(actiname=="all")
		{
			getAllUser(currentPage);　
		}
		else
		{
			selectUserByActivity(currentPage,actiname)
		}
	});
});

function getAllUser(index) {

	$.ajax({
		type: 'POST',
		url: 'http://localhost:9999/pageUserList',
		data: {
			"index": index*count,
			"count": count
		},
		dataType: 'JSON',
		success: function(data) {
			var nowpage=currentPage+1;
			document.getElementById("pagenumber").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;"+nowpage+"&nbsp;&nbsp;&nbsp;&nbsp;";

            if(data.length==0)
            {
            	currentPage--;
            nowpage--;
            document.getElementById("pagenumber").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;"+nowpage+"&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            else
            {
            	$("#tableAllUser").remove();
			$("#divTable").append('<table id="tableAllUser"  border="1px" bordercolor="gray" class="table table-hover" cellspacing="0" cellpadding="0"><tr><th>用户ID</th><th>玩家姓名</th><th>密码</th><th>注册时间</th><th>更新时间</th><th>登录时间</th></tr></table>');

			for(var i = 0; i < data.length; i++) {
				$("#tableAllUser").append("<tr><td>" +
					data[i].user_id + "</td><td>" +
					data[i].user_name + "</td><td>" +
					data[i].user_password + "</td><td>" +
					data[i].register_timestamp + "</td><td>" +
					data[i].update_timestamp + "</td><td>" +
					data[i].login_timestamp + "</td></tr>");
			}
			
			//设置表头剧中对齐
			 let elements = document.querySelectorAll("#tableAllUser tr th");
             for(let i = 0; i < elements.length; i++) {
             elements[i].style.textAlign = "center";
             }
			
            }
		}
	})

}
//查询所有活动
function getAllActivity(){
	
	$.ajax({
		type: 'GET',
		url: 'http://localhost:9999/getAllActivity',
		dataType: 'JSON',
		data: {
			
		},
		success: function(data) {
            document.getElementById("current_acti").innerHTML="all<span class='caret'></span>";
			$("#selctMenue").empty();
			$("#selctMenue").append("<li><a href='#'>all</a></li>" );
			for(var i = 0; i < data.length; i++) {
				$("#selctMenue").append("<li><a href='#'>" +
					data[i] + "</a></li>" );
			}
			//绑定事件
			$("#selctMenue").find("a").on("click",function(){
				console.log(this);
				selectMapenueClick(0,this.innerHTML);
			});
        }
	})
}

function selectMapenueClick(index,activityName){
		currentPage=0;
		selectUserByActivity(index,activityName);
}


function selectUserByActivity(index,activityName){
	document.getElementById("current_acti").innerHTML=activityName+"<span class='caret'></span>";
	actiname=activityName;
	if(actiname=="all")
	{
		document.getElementById("current_acti").innerHTML="all<span class='caret'></span>";
		getAllUser(currentPage);
	}
	else
	{
		$.ajax({
		type: 'POST',
		url: 'http://localhost:9999/selectUserByActivity',
		dataType: 'JSON',
		data: {
			"index": index*count,
			"count": count,
			"activityName":activityName
		},
		success: function(data) {
			
            
            var nowpage=currentPage+1;
			document.getElementById("pagenumber").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;"+nowpage+"&nbsp;&nbsp;&nbsp;&nbsp;";

 	        
            if(data.length==0)
            {
            	if(currentPage==0)
            	{
            		   $("#tableAllUser").remove();
            		   document.getElementById("pagenumber").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;"+1+"&nbsp;&nbsp;&nbsp;&nbsp;";
            	}
            	else
            	{
            			currentPage--;
                    nowpage--;
                    document.getElementById("pagenumber").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;"+nowpage+"&nbsp;&nbsp;&nbsp;&nbsp;";
            	}
            
           
            }
            else
            {
            $("#tableAllUser").remove();
			$("#divTable").append('<table id="tableAllUser"  border="1px" bordercolor="gray" class="table table-hover" cellspacing="0" cellpadding="0"><tr><th>排名</th><th>手机号</th><th>玩家得分</th></tr></table>');

			for(var i = 0; i < data.length; i++) {
				$("#tableAllUser").append("<tr><td>" +
					(index*count+i+1)+ "</td><td>" +
					data[i].user_id + "</td><td>" +
					data[i].score + "</td></tr>");
			}
			
			//设置表头剧中对齐
			 let elements = document.querySelectorAll("#tableAllUser tr th");
             for(let i = 0; i < elements.length; i++) {
             elements[i].style.textAlign = "center";
             }
           }
          }
	 });
	}
}


